﻿///***************************************************************
/// 시  스  템 : Updms.Com
/// 단위시스템 : COM
/// 계      층 : Presentation Layer
/// 프로그램명 : Com3015p
/// 파  일  명 : Com3015p.aspx.cs
/// 설      명 : 인원검색
///***************************************************************
/// 수정ID 일자       이름     내용
///---------------------------------------------------------------
/// CH001  2013-06-20 송시명   최초 작성
///***************************************************************
using System;
using System.Data;
using System.Collections;
using System.Web.UI;
using System.Web.UI.WebControls;

using SMFramework.Data;
using SMFramework.Utility;
using SMFramework.Web.UI.WebControls.Page;

using Base.Dac;
using Updms.Com.Exe;


namespace Updms.Com
{
	public partial class Com3015p : Updms.Common.BaseClass.PageBase
	{
		#region 전역영역

		//프로그램상수
		public static readonly string PID = "Com3015p";
		public static readonly string gs_path_info = "/updms/Com/" + PID + ".aspx";

		Global global = new Global();
		AccessCheck access = new AccessCheck();

		//검색
		protected string kw1 = "";	//아이디
		protected string kw2 = "";	//이름
		protected string kw3 = "";	//이메일

		#endregion

		#region Page_Load, OnPageLoad

		protected void Page_Load(object sender, EventArgs e)
		{
			SetInit();
			if (!Page.IsPostBack)
			{
				// 최초 화면 로딩시 처리해야 할 부분
				OnPageLoad();
				SearchProcess();
			}
		}

		// OnPageLoad
		private void OnPageLoad()
		{
			SetLiterals(global.GetLabelInfoByPgmId(PID, LANG_SET));
			SetClientScript();
			SetSearchParameter();
		}

		#endregion

		#region InitializeComponent

		private void InitializeComponent()
		{
			this.Load += new System.EventHandler(this.Page_Load);
			this.PreRender += new System.EventHandler(this.Page_PreRender);
		}

		#endregion

		#region SetClientScript

		private void SetClientScript()
		{
			//onKeyPress이벤트 (엔터검색)
			txt_search_user_id.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_nm.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
			txt_search_email.Attributes.Add("onkeypress", "enterSearchOnPage(event)");
		}

		#endregion

		#region SetInit, SetSearchParameter, ShowKeywordLabel

		//초기값설정
		private void SetInit()
		{
			lbl_search_flag.Text = "INIT";
		}

		//검색파라미터
		private void SetSearchParameter()
		{
			switch (lbl_search_flag.Text.Trim().ToUpper())
			{
				case "INIT":
					if (String.IsNullOrEmpty(Request.QueryString["kw1"]))	// url에 값이 없으면
					{
						kw1 = txt_search_user_id.Text.Trim();
					}
					else
					{
						kw1 = Server.UrlDecode(Request.QueryString["kw1"]);
						txt_search_user_id.Text = kw1;
					}


					if (String.IsNullOrEmpty(Request.QueryString["search_keyword"]))	// url로 넘어온 검색어가 없으면
					{
						if (String.IsNullOrEmpty(Request.QueryString["kw2"]))	// url에 값이 없으면
						{
							kw2 = txt_search_nm.Text.Trim();
						}
						else
						{
							kw2 = Server.UrlDecode(Request.QueryString["kw2"]);
							txt_search_nm.Text = kw2;
						}
					}
					else
					{
						kw2 = Server.UrlDecode(Request.QueryString["search_keyword"]);
						txt_search_nm.Text = kw2;
					}

					if (String.IsNullOrEmpty(Request.QueryString["kw3"]))	// url에 값이 없으면
					{
						kw3 = txt_search_email.Text.Trim();
					}
					else
					{
						kw3 = Server.UrlDecode(Request.QueryString["kw3"]);
						txt_search_email.Text = kw3;
					}

					break;
				case "SEARCH":
					kw1 = txt_search_user_id.Text.Trim();
					kw2 = txt_search_nm.Text.Trim();
					kw3 = txt_search_email.Text.Trim();
					break;
			}
		}

		//검색키워드
		private void ShowKeywordLabel()
		{
			lbl_search_keyword.Text = kw1 + " / " + kw2 + " / " + kw3;
		}

		#endregion

		#region CRUD (SelectProcess, SearchProcess)

		//조회
		private void SelectProcess()
		{
			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			//검색
			lo_dp.AddProperty("ls_user_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_nm", typeof(string), kw2);
			lo_dp.AddProperty("ls_email", typeof(string), kw3);
			lo_dp.AddProperty("ls_use_yn", typeof(string), "");
			lo_dp.AddProperty("ls_lang_cd", typeof(string), LANG_SET);

			using (Com3015p_NT _Com3015p_NT = new Com3015p_NT())
			{
				lo_ds.Merge(_Com3015p_NT.SearchUserInfo(lo_dp));
			}

			//결과가 1개면 바로 부모페이지로
			int cnt = DataSetUtil.GetRowCount(lo_ds, "UPDMS_COM_USER_MGT");
			if (cnt == 1)
			{
				string user_id = DataSetUtil.GetFldStr(lo_ds,"UPDMS_COM_USER_MGT","User_Id");
				string nm = DataSetUtil.GetFldStr(lo_ds, "UPDMS_COM_USER_MGT", "Nm");
				string ls_msg = "opener.document.form1['txt_search_user_id'].value ='" + user_id + "';opener.document.form1['txt_search_nm'].value ='" + nm + "';window.close();";
				ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), null, ls_msg, true);
			}
			else
			{
				GridView1.DataSource = lo_ds;
				GridView1.DataBind();

				//결과출력(키워드, 검색행수)
				ShowKeywordLabel();
				lbl_result_count.Text = cnt.ToString();
			}
		}

		//검색
		private void SearchProcess()
		{
			lbl_search_flag.Text = "SEARCH";
			SetSearchParameter();
			SelectProcess();
		}

		#endregion

		#region Page_PreRender, ChangePageMode

		// Page_PreRender
		private void Page_PreRender(object sender, System.EventArgs e)
		{
			if (!Page.IsPostBack)
			{
				//접근권한 체크
				if (!access.CheckPgmAccessGrant(PID, USER_ID, ACCESS_LOG_INFO))
				{
					Response.Redirect(URL_NO_AUTHORITY_REDIRECT_POPUP);	//사용자의 권한은 요청하신 페이지를 볼 수 없습니다
				}

			}
			ChangePageMode();
		}

		// ChangePageMode
		private void ChangePageMode()
		{
			SetGridViewHeader(global.GetColumnListByPgmId(PID, LANG_SET));
		}

		#endregion

		#region GridView이벤트

		//RowDataBound
		protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
				//마우스올렸을 때
				e.Row.Attributes.Add("onmouseover", "overBackground('GridView1', " + (e.Row.RowIndex + 1) + ")");
				e.Row.Attributes.Add("onmouseout", "outBackground('GridView1', 0)");

				//번호
				e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
			}
		}

		//RowCommand
		protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			GridView gv = (GridView)sender;
			GridViewRow row = (GridViewRow)(((Control)e.CommandSource).NamingContainer);

			//Template
			LinkButton lbtn_rc = (LinkButton)GridView1.Rows[row.RowIndex].FindControl("lbtn_rc");

			string user_id = lbtn_rc.Text;
			string nm = row.Cells[2].Text.ToString();

			string ls_msg = "opener.document.form1['txt_search_user_id'].value ='" + user_id + "';opener.document.form1['txt_search_nm'].value ='" + nm + "';window.close();";
			ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), null, ls_msg, true);
		}

		#endregion

		#region 버튼이벤트

		//검색
		protected void ibtn_search_Click(object sender, EventArgs e)
		{
			SearchProcess();
		}

		//엑셀
		protected void ibtn_excel_Click(object sender, EventArgs e)
		{
			SetSearchParameter();

			DataSet lo_ds = new DataSet();
			DataPack lo_dp = new DataPack();

			lo_dp.AddProperty("ls_user_id", typeof(string), kw1);
			lo_dp.AddProperty("ls_nm", typeof(string), kw2);
			lo_dp.AddProperty("ls_email", typeof(string), kw3);
			lo_dp.AddProperty("ls_use_yn", typeof(string), "");
			lo_dp.AddProperty("ls_lang_cd", typeof(string), LANG_SET);

			using (Com3015p_NT _Com3015p_NT = new Com3015p_NT())
			{
				lo_ds.Merge(_Com3015p_NT.SearchUserInfo(lo_dp));
			}

			string fileName = PID + "(" + global.GetMenuTitle(PID, LANG_SET) + ")";
			ExcelUtil.CreateExcelFileX(this.Page, fileName, lo_ds, SetExcelHeaderX(global.GetColumnListByPgmId(PID, LANG_SET)));
		}

		#endregion

	}
}
